<style scoped>
    .layui-form >>> .layui-form-select{
        z-index: inherit;
    }
</style>

<template>
    <div class="company">
        <fieldset class="layui-elem-field layui-field-title">
            <legend>添加种植批次</legend>
        </fieldset>

        <form class="layui-form " >
            <input type="hidden" name="status" placeholder="" autocomplete="off" :value="1" class="layui-input">
            <div class="layui-form-item">
                <label class="layui-form-label">批次名称</label>
                <div class="layui-input-block">
                    <input type="text" name="name"  required lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">所属部门</label>
                <div class="layui-input-block">
                    <select type="text" name="deptguid" lay-verify="required" class="layui-select" >
                        <option v-for="(depart, index) in departments" :value="depart.guid" >{{depart.name}}</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">备注</label>
                <div class="layui-input-block">
                    <input type="text" name="remark" placeholder="" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="submit">立即提交</button>
                    <button class="layui-btn layui-btn-primary" @click="$parent.back">返回</button>
                </div>
            </div>
        </form>
    </div>
</template>

<script>
    export default {
        name: 'AddFarmerbatch',
        data () {
            return {
                departments: ''
            }
        },
        mounted() {
            let that = this;
            layui.use(['form'], function () {
                var form = layui.form,
                    layer = layui.layer;


                //提交编辑过的数据
                form.on('submit(submit)', function (data) {
                    let loader = layer.load();
                    $.ajax({
                        url: '/farmer/Farmerbatch!save.action',
                        data: data.field,
                        type: "post",
                        dataType: "json",
                        success: function(res) {
                            if (res.info == "success") {
                                that.toast("添加成功");
                                that.$parent.switchPage('Farmerbatch');
                            } else if (res.info == 'ishave') {
                                that.toastText("批次名称已存在");
                            } else{
                                that.toast("系统错误");
                            }
                        },
                        complete: function () {
                            layer.close(loader);
                        }
                    });
                    return false;
                });
            });
            this.getTypes();
        },
        updated() {
            layui.form.render();
        },
        methods: {
            back: function () {
                this.$parent.switchPage(this.$parent.refView);
            },
            getTypes: function() {
                this.axios({
                    method: 'get',
                    responseType: 'json',
                    url: '/sales/Sellform!findDepartment.action',
                    params: {sPermission: 41}
                }).then(response => {
                    if (response.data.status == "true") {
                        this.departments = response.data.result;
                    } else if (response.data.status == "false") {
                        this.toastText(response.data.result);
                    }  else {
                        this.toastText('获取部门数据失败');
                    }
                });
            }

        }
    }
</script>
